package bd403.Backend;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/*
 * A User represents a user of the BrewDawg application. It allows the app to
 * look up the preferences of a particular user and get his/her favorite drinks,
 * as specified by the user.
 * Main author: Joe
 */
public class User {
	
	private String username;
	private Map<String, Drink> favorites;

	/*
	* Creates a new User with given username
	* Assumes proper username input
	* Proper input = strictly lowercase characters, pattern [a-z]+
	*/
	public User(String user) {
		username = parseInput(user);
		favorites = new TreeMap<String, Drink>();
	}
	
	/*
	* Returns an iterator containing Drinks that have been marked as favorite
	* by this user. 
	*/
	public Iterator<Drink> getFavorites() {
		return favorites.values().iterator();
	}
	
	/* Returns true if a drink with the name drinkName already exists
	 * in THIS user's favorites
	 */
	protected boolean favoriteExists(String drinkName) {
		return favorites.containsKey(drinkName);
	}
	
	/* Adds a given Drink to the User's list of favorite Drinks
	 *  Note: a precondition is that the Drink d is already a legal Drink as specified by the application
	 */
	public void addToFavorites(Drink d) {
		if (!favorites.containsKey(d.getName())) {
			favorites.put(d.getName(), d);
		}
	}
	
	/*
	* Returns a String representing the user name of THIS
	*/
	public String getUsername() {
		return username;
	}
	
	/*
	 * Helper method to trim the input properly.
	 */
	private String parseInput(String name) {
		//Replace all non-word characters with nothing. This removes spaces too
		return name.replaceAll("\\W", "");
	}
}
